package com.kuaiying.utils;

import java.awt.Color;
import java.io.InputStream;
import java.util.Random;
import java.util.logging.Logger;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.Font;
import com.aspose.words.HeaderFooter;
import com.aspose.words.HeaderFooterType;
import com.aspose.words.License;
import com.aspose.words.Node;
import com.aspose.words.NodeCollection;
import com.aspose.words.NodeType;
import com.aspose.words.PageSetup;
import com.aspose.words.Paragraph;
import com.aspose.words.ParagraphAlignment;
import com.aspose.words.Range;
import com.aspose.words.Run;
import com.aspose.words.SaveFormat;
import com.aspose.words.Section;

public class AsposeUtils {
	private static License aposeLic = null;
	private static Logger log = Logger.getLogger(AsposeUtils.class.getName());
	static {
		if (getLicense()) {
			log.info("License Success...");
		} else {
			log.info("License Failed...");
			System.exit(0);
		}
	}

	/**
	 * 获取license
	 * 
	 * @return
	 */
	public static boolean getLicense() {
		boolean result = false;
		InputStream is = AsposeUtils.class.getClassLoader().getResourceAsStream(
				"license.xml");
		aposeLic = new License();
		try {
			aposeLic.setLicense(is);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void delHeader(String filename, String outname) {
		try {
			log.info("file:" + filename );
			Document doc = new Document(filename);
			removewatermarkAll(doc);
			insertHeader(doc, "asdas");
			insertFooter(doc, "foot ...");
			doc.save(outname);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 移除页脚
	 * @param doc
	 */
	public static void removefootAll(Document doc) {
		for (Section section : doc.getSections()) {
			HeaderFooter footer;
			footer = section.getHeadersFooters().getByHeaderFooterType(
					HeaderFooterType.FOOTER_FIRST);
			if (footer != null)
				footer.remove();
			// Primary footer is the footer used for odd pages.
			footer = section.getHeadersFooters().getByHeaderFooterType(
					HeaderFooterType.FOOTER_PRIMARY);
			if (footer != null)
				footer.remove();
			footer = section.getHeadersFooters().getByHeaderFooterType(
					HeaderFooterType.FOOTER_EVEN);
			if (footer != null)
				footer.remove();
		}
	}
	
	/**
	 * 移除指定Section的水印、页眉、页脚
	 * 
	 * @param doc
	 * @throws Exception
	 */
	private static void removewatermarkAll(Document doc) throws Exception {
		for (Section section : doc.getSections()) {
			removeWatermarkFromHeader(section, HeaderFooterType.HEADER_PRIMARY);
			removeWatermarkFromHeader(section, HeaderFooterType.HEADER_FIRST);
			removeWatermarkFromHeader(section, HeaderFooterType.HEADER_EVEN);
			removeWatermarkFromHeader(section, HeaderFooterType.FOOTER_PRIMARY);
			removeWatermarkFromHeader(section, HeaderFooterType.FOOTER_FIRST);
			removeWatermarkFromHeader(section, HeaderFooterType.FOOTER_EVEN);
		}
	}

	/**
	 * 移除指定Section的水印、页眉、页脚
	 * 
	 * @param sect
	 * @param headerType
	 * @throws Exception
	 */
	private static void removeWatermarkFromHeader(Section sect, int headerType)
			throws Exception {
		HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(
				headerType);
		if (header != null) {
			header.removeAllChildren();
		}
	}
	/**
	 * 插入页眉
	 * @param doc
	 * @param header
	 * @throws Exception 
	 */
	public static void insertHeader(Document doc, String header) throws Exception{
		DocumentBuilder builder = new DocumentBuilder(doc);
		builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
		builder.write( header );
	}
	
	/**
	 * 插入页脚
	 * @param doc
	 * @param header
	 * @throws Exception 
	 */
	public static void insertFooter(Document doc, String footer) throws Exception{
		DocumentBuilder builder = new DocumentBuilder(doc);
		builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
		builder.write( footer );
	}
	
	/**
	 * 将word转成其他文件格式
	 * @param srcUrl
	 * @param fileType
	 */
	public static void wordToOther(String srcUrl, String fileType){
		if(getLicense()){
			try {
				Document doc = new Document(srcUrl);
				doc.save(srcUrl + "." + fileType);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		AsposeUtils model = new AsposeUtils();
		String filepath = "D:\\docin\\edudown\\";
		String savePath = "D:\\docin\\edudown\\";
		String input = "函数模型及其应用.doc";
		String output = "函数模型及其应用p.doc";
	}
}
